package swu.edu.cn.javaee.service;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import swu.edu.cn.javaee.entity.*;
import swu.edu.cn.javaee.mapper.*;

import static swu.edu.cn.javaee.component.Signal.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CollectService {
    @Resource
    private CollectMapper collectMapper;
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private MerchantMapper merchantMapper;
    @Resource
    private ProductMapper productMapper;
    //创建文章收藏
    public String articleCollect(String user_id,String article_id){
        Collect article_collect=new Collect();
        Date creation_time=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Article article=articleMapper.queryArticleById(article_id);
        if (article!=null){
            article_collect.setUser_id(user_id);
            article_collect.setArticle_id(article_id);
            article_collect.setCollect_date(format.format(creation_time));
            collectMapper.articleCollect(article_collect);
            System.out.println(article_collect);
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_Article_NO_EXIST;
        }

    }

    //评论收藏
    public String reviewCollect(String user_id,String review_id){
        Collect review_collect=new Collect();
        Date creation_time=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Comment comment=commentMapper.queryCommentById(review_id);
        if (comment!=null){
            review_collect.setUser_id(user_id);
            review_collect.setReview_id(review_id);
            review_collect.setCollect_date(format.format(creation_time));
            collectMapper.reviewCollect(review_collect);
            System.out.println(review_collect);
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_NO_COMMENT;
        }
    }

    //商户收藏
    public String merchantCollect(String user_id,String merchant_id){
        Collect merchant_collect=new Collect();
        Date creation_time=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Merchant merchant=merchantMapper.queryByCid(merchant_id);
        if (merchant!=null){
            merchant_collect.setUser_id(user_id);
            merchant_collect.setMerchant_id(merchant_id);
            merchant_collect.setCollect_date(format.format(creation_time));
            collectMapper.merchantCollect(merchant_collect);
            System.out.println(merchant_collect);
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_MERCHANT_NO_EXIST;
        }
    }

    //商品收藏
    public String productCollect(String user_id,String product_id){
        Collect product_collect=new Collect();
        Date creation_time=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Product product=productMapper.queryPid(product_id);
        if (product!=null){
            product_collect.setUser_id(user_id);
            product_collect.setProduct_id(product_id);
            product_collect.setCollect_date(format.format(creation_time));
            collectMapper.productCollect(product_collect);
            System.out.println(product_collect);
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_PRODUCT_NO_EXIST;
        }
    }


    //展示用户文章收藏
    public CollectInfo queryArticleList(String user_id){
        List<Collect> articleList=collectMapper.queryArticleList(user_id);
        Integer count=collectMapper.countArticle(user_id);
        CollectInfo articleInfo=new CollectInfo();
        if(articleList.size()!=0){
            articleInfo.setCode("0");
            articleInfo.setCount(String.valueOf(count));
            articleInfo.setMsg("");
            articleInfo.setData(articleList);
            return articleInfo;
        }
        else {
            return null;
        }

    }


    //展示用户评论收藏
    public CollectInfo queryReviewList(String user_id){
        List<Collect> reviewList=collectMapper.queryReviewList(user_id);
        Integer count=collectMapper.countReview(user_id);
        CollectInfo reviewInfo=new CollectInfo();
        if(reviewList.size()!=0){
            reviewInfo.setCode("0");
            reviewInfo.setCount(String.valueOf(count));
            reviewInfo.setMsg("");
            reviewInfo.setData(reviewList);
            return reviewInfo;
        }
        else {
            return null;
        }
    }

    //展示用户商户收藏
    public CollectInfo queryMerchantList(String user_id){
        List<Collect> merchantList=collectMapper.queryMerchantList(user_id);
        Integer count=collectMapper.countMerchant(user_id);
        CollectInfo merchantInfo=new CollectInfo();
        if(merchantList.size()!=0){
            merchantInfo.setCode("0");
            merchantInfo.setCount(String.valueOf(count));
            merchantInfo.setMsg("");
            merchantInfo.setData(merchantList);
            return merchantInfo;
        }
        else {
            return null;
        }
    }

    //展示用户产品收藏
    public CollectInfo queryProductList(String user_id){
        List<Collect> productList=collectMapper.queryProductList(user_id);
        Integer count=collectMapper.countProduct(user_id);
        CollectInfo productInfo=new CollectInfo();
        if(productList.size()!=0){
            productInfo.setCode("0");
            productInfo.setCount(String.valueOf(count));
            productInfo.setMsg("");
            productInfo.setData(productList);
            return productInfo;
        }
        else {
            return null;
        }
    }



    //删除文章收藏
    public String deleteArticle(String user_id,String article_id){
        collectMapper.deleteArticle(user_id,article_id);
        return SUCCESS_CODE;
    }
    //删除评论收藏
    public String deleteReview(String user_id,String review_id){
        collectMapper.deleteReview(user_id,review_id);
        return SUCCESS_CODE;
    }
    //删除商户收藏
    public String deleteMerchant(String user_id,String merchant_id){
        collectMapper.deleteMerchant(user_id,merchant_id);
        return SUCCESS_CODE;
    }
    //删除商品收藏
    public String deleteProduct(String user_id,String product_id){
        collectMapper.deleteProduct(user_id,product_id);
        return SUCCESS_CODE;
    }


}
